/*
 * Golazo GeoNotes
 * Copyright (C) 2009 Nakajiman Software Inc.
 * http://nakajiman.lrlab.to/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * serverload
 */
function serverload() {

  // GET
  Golazo.dispatch(function(request, response, me) {

	var startIndex = request.data.startIndex || '';
	if (!Golazo.checkDigit(startIndex))
	  startIndex = 1;
	else
	  startIndex = parseInt(startIndex, 10);
	if (startIndex < 1)
	  startIndex = 1;

    var maxResults = 10;

    var notes = Golazo.Note.find({
	  order: 'updated DESC', offset: startIndex - 1, limit: maxResults + 1
	});

	var totalResults = startIndex - 1 + notes.length;

	notes = $.map(notes.slice(0, maxResults), function(note) {
      note = note.objectify(true);
	  note.user.isMe = note.user.id === (me && me.id);
	  return note;
	});

	if (me && me.id)
	  $(document).me(me);

	$.each(notes, function(i, note) {
	  $('#notes div.note:hidden').clone()
	    .note(note).appendTo('#notes').show();
	});

    var newerIndex = startIndex - maxResults;
	if (newerIndex >= 1)
	  $('#pager a.newer').attr('href', '?startIndex='+newerIndex).show();

    var olderIndex = startIndex + maxResults;
	if (olderIndex <= totalResults)
	  $('#pager a.older').attr('href', '?startIndex='+olderIndex).show();

  // POST
  }, function(request, response, me) {
    throw 'Method Not Allowed';
  });
};

/**
 * load
 */
function load() {
  
  $('#notes div.note').deleteNote();
  
  //$('#friends').gadget('http://gadgets.lrlab.to/friendconnect/friends.site.xml', {});
  $('#friends').gadget('http://www.google.com/friendconnect/gadgets/members.xml', {
	skin: $.extend({ 
	  NUMBER_ROWS: 10
	}, Golazo.GOOGLE_FCSKIN)
  });

  $('#recommended').gadget('http://www.google.com/friendconnect/gadgets/recommended_pages.xml', {
	skin: $.extend({ 
	  HEADER_TEXT: 'Recommended notes',
	  RECOMMENDATIONS_PER_PAGE: 10
	}, Golazo.GOOGLE_FCSKIN),
    docId: 'recommendedPages'
  });

};
load.runat = 'client';